/**
 * Created by WangMing on 15/12/9.
 */
define(["../../../service",
    "../../../widgets/player_live_widget/player_live_widget"
], function(api) {
    // 定义所有相关的vmodelplayerlivewidgetplayerlivewidget
    var vm = avalon.define({
        $id: "live",
        $player: {
            stateName :"lives.item"
        },

        $breadcrumb: {
            stateName: "lives.item"
        },
        $skipArry: ["playerWidget"],
        getLivePath: function() {

            api.getItems({
                method: api.getLiveuri,
                query: {
                    id: vm.itemID,
                }
            }, function(res) {
                if (!vm.$player.VideoResourcePath) vm.$player.VideoResourcePath = {};
                vm.$player.VideoResourcePath = _.merge(vm.$player.VideoResourcePath, res);
                
                // setTimeout(function(){
                    vm.$fire("down!videopathUpdate",vm.$player)
                // }, 1000)
            })

        }
    });

    return avalon.controller(function($ctrl) {
        // 视图渲染后，意思是avalon.scan完成
        $ctrl.$onRendered = function() {
            vm.getLivePath()

        };
        // 进入视图
        $ctrl.$onEnter = function(param, rs, rj) {
            vm.itemID = param.itemID;
            return new Promise(function(resolve, reject) {
                    resolve()
                }).then(function() {
                    return api.getItems({
                        method: api.getLive,
                        query: {
                            id: param.itemID
                        }
                    }, function(res) {
                        if (!vm.$player.VideoInfo) vm.$player.VideoInfo = {};
                        vm.$player.VideoInfo = _.merge(vm.$player.VideoInfo, res.data)
                    })

                }).then(function() {
                    return api.getItems({
                        method: api.getLiveAttachment,
                        query: {
                            liveId: param.itemID
                        }
                    }, function(res) {
                        if (!vm.$player.LiveAttachment) vm.$player.LiveAttachment = [];
                        vm.$player.LiveAttachment = _.merge(vm.$player.LiveAttachment, res.list)
                    })

                })
            
        };
        // 对应的视图销毁前
        $ctrl.$onBeforeUnload = function() {

        };
        $ctrl.$vmodels = [vm];
    })
});
